<?php
require('functions/page_load.php');

$settings_nav = true;

if(isset($_GET['delete'])){

	$sql = "DELETE FROM sms_packages WHERE id = '".mysql_real_escape_string($_GET['delete'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$results = '<div class="success_result">SMS package deleted successfully.</div>';

}

if($_SERVER['REQUEST_METHOD'] == 'POST'){

	if(isset($_GET['id'])){
	
	$sql = "UPDATE sms_packages SET sms = '".mysql_real_escape_string($_POST['sms'])."', price = '".mysql_real_escape_string($_POST['price'])."' WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$results = '<div class="success_result">SMS package updated successfully.</div>';	
	
	}
	
	else{
	
	$sql = "INSERT INTO sms_packages SET sms = '".mysql_real_escape_string($_POST['sms'])."', price = '".mysql_real_escape_string($_POST['price'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$results = '<div class="success_result">SMS package created successfully.</div>';
	
	}

}



	$sql = "SELECT id, sms, price FROM sms_packages ORDER BY sms";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rows = mysql_num_rows($query);
	
	if($rows > 0){
	
	$content = '<table width="100%" cellpadding="0" cellspacing="0" class="cms_table"><thead><tr><th align="left">SMS</th><th align="left">Cost</th><th width="60">Edit</th><th width="60">Delete</th></tr></thead><tbody>';
	
		while($rs=mysql_fetch_assoc($query)){
		
		$content.='<tr><td align="left">'.$rs['sms'].' SMS</td><td align="left">&pound;'.$rs['price'].'</td><td><a href="?id='.$rs['id'].'" title="Edit"><img src="images/edit.png" alt="edit" /></a></td><td><a onclick="confirm_delete(\'?delete='.$rs['id'].'\',\'this sms package\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';
		
		}
		
	$content.='</tbody></table>';
		
	}
	
	else{
	
	$content = '<p style="float: left; width: 100%; text-align: center;">There are currently no SMS packages.</p>';
	
	}
	
if(isset($_GET['id'])){
	
$sql = "SELECT sms, price FROM sms_packages WHERE id = '".mysql_real_escape_string($_GET['id'])."'";
$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
$rs = mysql_fetch_assoc($query);

}

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Navigation</h2>

<ul>
<li><a href="banners.php">Banners</a></li>
<li><a href="trades.php">Trades</a></li>
<li><a href="directory_categories.php">Directory Categories</a></li>
<li><a href="subscriptions.php">Subscriptions</a></li>
<li><a href="sms_packages.php">SMS Packages</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">SMS Packages</h2>
	
	<?php if(isset($_GET['id'])){ ?>
	<a href="sms_packages.php" class="create_button">Create new SMS Package</a>
	<?php } ?>
	
	<?php echo $results; ?>
	
	<form method="post" action="">

			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="sms">SMS:</label></div>
			<div class="form_row_right"><input type="text" name="sms" id="sms" class="textbox" value="<?php echo $rs['sms']; ?>" style="width: 80px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="price">Cost:</label></div>
			<div class="form_row_right"><input type="text" name="price" id="price" class="textbox" value="<?php echo $rs['price']; ?>" style="width: 80px;" /><span style="margin-left: 10px;">Only use numbers and decimals, do not include a pound sign</span></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>

	<?php echo $content; ?>

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>